package com.luych.toolbox.common.event.base.service.publisher;

import com.luych.toolbox.common.event.base.service.WebSocketNoticeRemoteApplicationEvent;
import com.luych.toolbox.common.feign.param.gateway.center.WebSocketNotice;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;

public class WebSocketNoticePublisher {

    private final BusProperties busProperties;
    private final ApplicationEventPublisher applicationEventPublisher;

    public WebSocketNoticePublisher(BusProperties busProperties, ApplicationEventPublisher applicationEventPublisher) {
        this.busProperties = busProperties;
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void info(String userId, String message) {
        WebSocketNotice notice = new WebSocketNotice(WebSocketNotice.Type.INFO, message);
        this.send(userId, notice);
    }

    public void warn(String userId, String message) {
        WebSocketNotice notice = new WebSocketNotice(WebSocketNotice.Type.WARN, message);
        this.send(userId, notice);
    }

    public void error(String userId, String message) {
        WebSocketNotice notice = new WebSocketNotice(WebSocketNotice.Type.ERROR, message);
        this.send(userId, notice);
    }

    public void success(String userId, String message) {
        WebSocketNotice notice = new WebSocketNotice(WebSocketNotice.Type.SUCCESS, message);
        this.send(userId, notice);
    }

    public void send(String userId, WebSocketNotice.Type type, String message, Object data) {
        WebSocketNotice notice = new WebSocketNotice(type, message);
        notice.setData(data);
        this.send(userId, notice);
    }

    public void send(String userId, WebSocketNotice.Type type, String message) {
        WebSocketNotice notice = new WebSocketNotice(type, message);
        this.send(userId, notice);
    }

    public void send(String userId, WebSocketNotice notice) {
        WebSocketNoticeRemoteApplicationEvent event = new WebSocketNoticeRemoteApplicationEvent(busProperties.getId(), "**", userId, notice);
        applicationEventPublisher.publishEvent(event);
    }
}

